package com.sky.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.Phaser;
import java.util.function.Consumer;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealList = setmealMapper.selectPage(setmealPageQueryDTO);
        PageInfo<SetmealVO> pageInfo = new PageInfo<>(setmealList);
        PageResult pb = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pb);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());

        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.insert(setmeal);
        System.out.println(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(new Consumer<SetmealDish>() {
            @Override
            public void accept(SetmealDish setmealDish) {
                setmealDish.setSetmealId(setmeal.getId());
                System.out.println(setmealDish);
            }
        });


        setmealDishMapper.insert(setmealDishes);

    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        setmealMapper.updateStatus(status,id);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public Result deleteByIds(List<Long> ids) {
        Integer status = 0;
        for (Long id : ids) {
           SetmealVO setmealVO = setmealMapper.selectById(id);
           status = setmealVO.getStatus();
        }
        if (status == 1) {
            return Result.error("套餐正在售卖中，无法删除");
        } else {
            setmealMapper.deleteByIds(ids);
            return Result.success();
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Result<SetmealVO> selectById(Long id) {
       SetmealVO setmealVO = setmealMapper.selectById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public Result update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.update(setmeal);
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
        return Result.success();
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public Result<List<Setmeal>> selectByCategoryId(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.selectByCategoryId(categoryId);
        return Result.success(setmealList);
    }

    /**
     * 根据套餐id查询套餐
     * @param setmealId
     * @return
     */
    @Override
    public Result<List<DishItemVO>> selectItemById(Long setmealId) {
        List<DishItemVO> dishItemVOList = setmealMapper.selectItemById(setmealId);
        return Result.success(dishItemVOList);
    }
}
